<?php

/*

  Action	$4

  +$1; +1 VP token
  Trash a card from your hand.  +VP tokens equal to half its cost in coins, rounded down. Each other player may trash a card from his hand.

 */

class Bishop extends Card implements iAction, iKingdom, iProsperity {

    public function __construct() {
        parent::__construct("Bishop", 4);
        $this->undoPlay = false;
        $this->undoBuy = true;
    }

    public function getDescription() {
        return array(
            "+$1",
            "+1 VP token",
            "Trash a card from your hand.  +VP tokens equal to half its cost in coins, rounded down. Each other player may trash a card from his hand.");
    }

    public function play(Game $game, $isRealCard) {
        $game->addMoney(new Price(1));
        ScreenDisplay::addScreenMessage($game, true, "get +1$", "gets +1$");
        $playHand = $game->getPlayHand();

        //first we set all other players, since they call is async.
        $names = $game->getOtherPlayersNames();
        foreach ($names as $name) {
            $otherPlayHand = $game->getPlayHand($name);
            $otherPlayHand = getUniqueCards($otherPlayHand);
            if (sizeof($otherPlayHand) > 0) {

                $otherPlayHand[] = new Option(1, "None");
                $output = MySocket::GetUserReply($game, Input_ChooseOne, $otherPlayHand, 1, 1, null, "Choose card to trash", $name, true);
            }
        }

        //now we check this user call, and we pick it up first.
        if (sizeof($playHand) > 0) {
            $playHand = getUniqueCards($playHand);
            if (sizeof($playHand) > 1) {
                $output = MySocket::GetUserReply($game, Input_ChooseOne, $playHand, 1, 1, null, "Choose card to trash");
                $card_name = $output[0];
            } else {
                $card_name = $playHand[0];
            }
            $card = $game->moveCardFromPileToPile($card_name, PlayHand, Trash);

            $price = $card->getPrice($game);
            $coins = $price->getCoins();
            $vps = 1 + floor($coins / 2);
            $game->addVictoryTokens($vps);
            ScreenDisplay::addScreenMessage($game, true, "trash " . nicePrint($card) . " and get $vps victory tokens", "trashes " . ScreenDisplay::$LMSG . " and gets $vps victory tokens");
        }
        else {
            $game->addVictoryTokens(1);
            ScreenDisplay::addScreenMessage($game, true, "trash nothing and get 1 victory token", "trashes noting and gets 1 victory token");
        }

        $game->setPhase(Phase_Sleep);

        //the waiting function
        $users_replies = MySocket::getUsersReplies($game);
        foreach ($users_replies as $name => $output) {
            $card_name = $output[0];
            if ($card_name != "None") {
                $card = $game->moveCardFromPileToPile($card_name, PlayHand, Trash, $name);
                ScreenDisplay::addScreenMessage($game, true, "trash " . nicePrint($card), "trashes " . ScreenDisplay::$LMSG, $name);
            } else {
                ScreenDisplay::addScreenMessage($game, true, "trash nothing", "trashes nothing", $name);
            }
            $game->setPhase(Phase_Sleep, $name);
        }

        $game->cardActionFinished();
        return true;
    }

}

?>